
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.Timer;
import Pieces.Board;

/**
 * {@code Game}
 * 
 * @version 10.09.09
 * @author Somebody
 */
public class Game {
	
	/*
	 * Timing stuff - not sure if would want to keep track of
	 * additional timing data if you're using a timer.
	 * 
	 * IIRC System.currentTimeMillis() is low-resolution.
	 */
	private Timer 	t;
	private int 	timeInt = 200;
	private long 	oldTime = System.currentTimeMillis();
	private long 	newTime;
	
	private Board 	gameBoard;
	private GUI 	gui;
	
	//private GameState state;
    
	public static void main(String[] args) {
		new Game();
	}

	public Game() {
		gameBoard = new Board();
		gui = new GUI(gameBoard);

		t = new Timer(timeInt, new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gameLoop();
			}
		});

		t.start();
	}

	public void gameLoop() {
		if (!gui.getPause()) {
			newTime = System.currentTimeMillis();
			int button = gui.getButton();
			if (button != KeyEvent.VK_DOWN) {
				timeInt = 200;
			}
			if (button == KeyEvent.VK_UP) {

				// check rotate collision
				gameBoard.rotateActivePiece();
			} else if (button == KeyEvent.VK_LEFT) {

				// check left collision
				// move left
				gameBoard.movePiece(-1); // move one to the left
			} else if (button == KeyEvent.VK_RIGHT) {

				// check right collision
				// move right
				gameBoard.movePiece(1); // move one to the right
			} else if (button == KeyEvent.VK_DOWN) {

				// check down collision
				// instant drop
				timeInt = 0;

				// get next piece
				// create new next piece
				// put new active piece on the board
			} else if (button == KeyEvent.VK_SPACE) {
				timeInt = 200;

				gameBoard.instantDrop();
			}

			if (newTime > oldTime + timeInt) {
				oldTime = newTime;
				// check blob collision
				// if(!collision){
				gameBoard.dropActivePiece();
			}
			// else{

			// gameBoard.activePiece = gameBoard.nextPiece;
			// gameBoard.nextPiece = gameBoard.getRandomPiece();
			// }
			// gui.draw();
			gui.showBoard();
		}
	}
}